﻿using Infrastructure.Attribute;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZR.Model;
using ZR.Model.Dto.Business.ProductionControl;
using ZR.Model.Models.ProductionControl;
using ZR.Repository;
using ZR.Service.IService.ProductionControl;

namespace ZR.Service.Service.ProductionControl
{
    /// <summary>
    /// 报警信息配置表Service业务层处理
    /// </summary>
    [AppService(ServiceType = typeof(ICksAlarmRecordService), ServiceLifetime = LifeTime.Transient)]
    public class CksAlarmRecordService : BaseService<CksAlarmRecord>, ICksAlarmRecordService
    {

        /// <summary>
        /// 查询列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public PagedInfo<CksAlarmRecordDto> GetList(CksAlarmRecordQueryDto parm) {

            var response = Queryable()
                 .LeftJoin<PlcConfig>((recDt, plcDt) => recDt.PlcID == plcDt.Id)
                 .LeftJoin<CksAlarmMessageConfig>((recDt, plcDt, messageDt) => recDt.PlcID == messageDt.PlcID && recDt.BlockAddr == messageDt.BlockAddr && recDt.AlarmAddr == messageDt.AlarmAddr && recDt.AlarmBitIndex == messageDt.AlarmBitIndex)
                 //产线查询
                 .WhereIF(!string.IsNullOrEmpty(parm.LineCode), (recDt, plcDt, messageDt) => plcDt.ProductionLineCode == parm.LineCode)
                 //站点查询
                 .WhereIF(!string.IsNullOrEmpty(parm.StationCode), (recDt, plcDt, messageDt) => plcDt.WorkstationCode == parm.StationCode)
                 //.WhereIF(parm.BeginTime != null && parm.EndTime == null, (recDt, plcDt, messageDt) => recDt.StartTime >= parm.BeginTime)
                 //.WhereIF(parm.BeginTime == null && parm.EndTime != null, (recDt, plcDt, messageDt) => recDt.EndTime <= parm.EndTime)
                 //时间段查询
                 .WhereIF(parm.BeginTime != null && parm.EndTime != null, (recDt, plcDt, messageDt) => recDt.StartTime >= parm.BeginTime && recDt.EndTime <= parm.EndTime)
                 //报警信息查询
                 .WhereIF(!string.IsNullOrEmpty(parm.SearchContent), (recDt, plcDt, messageDt) => messageDt.MessageCn.Contains(parm.SearchContent) || messageDt.MessageEs.Contains(parm.SearchContent) || messageDt.MessageUs.Contains(parm.SearchContent))
                 //.OrderByDescending((recDt, plcDt, messageDt) => recDt.Id)
                 .Select((recDt, plcDt, messageDt) => new CksAlarmRecordDto
                 {
                     PlcID = recDt.PlcID,
                     ProductionLineCode = plcDt.ProductionLineCode,
                     StationCode = plcDt.WorkstationCode,
                     AlarmRecordId = recDt.Id,
                     AlarmMessageId = messageDt != null ? messageDt.Id : 0,
                     BlockAddr = recDt.BlockAddr,
                     AlarmAddr = recDt.AlarmAddr,
                     AlarmBitIndex = recDt.AlarmBitIndex,
                     AlarmDBAddr = $"DB{recDt.BlockAddr}.{recDt.AlarmAddr}.{recDt.AlarmBitIndex}",
                     StartTime = recDt.StartTime,
                     EndTime = recDt.EndTime,
                     Value = recDt.Value,
                     Level = messageDt.Level,
                     MessageCn = messageDt.MessageCn ?? "-",
                     MessageEs = messageDt.MessageEs ?? "-",
                     MessageUs = messageDt.MessageUs ?? "-"
                 }).ToPage(parm);

            return response;
        }

    }

}
